import * as MediaLibrary from 'expo-media-library';
import { Alert } from 'react-native';

export const debugMediaPermissions = async () => {
  try {
    console.log('=== 开始调试媒体库权限 ===');
    
    // 1. 检查当前权限状态
    const permissionStatus = await MediaLibrary.getPermissionsAsync();
    console.log('当前权限状态:', permissionStatus);
    
    // 2. 请求权限
    const requestResult = await MediaLibrary.requestPermissionsAsync();
    console.log('权限请求结果:', requestResult);
    
    if (requestResult.status === 'granted') {
      // 3. 尝试获取媒体库信息
      const albums = await MediaLibrary.getAlbumsAsync();
      console.log('相册数量:', albums.length);
      console.log('所有相册列表:', albums);
      
             // 检查每个相册的内容
       for (const album of albums) {
         console.log(`相册 "${album.title}" (${album.id}):`, {
           assetCount: album.assetCount,
           type: album.type
         });
         
         // 获取相册内的资产
         const albumAssets = await MediaLibrary.getAssetsAsync({
           first: 50,
           album: album.id,
         });
         console.log(`相册 "${album.title}" 内的文件数:`, albumAssets.assets.length);
         
         // 详细打印每个文件的信息
         if (albumAssets.assets.length > 0) {
           console.log(`=== 相册 "${album.title}" 中的文件详情 ===`);
           albumAssets.assets.forEach((asset, index) => {
             console.log(`文件 ${index + 1}:`, {
               id: asset.id,
               filename: asset.filename,
               uri: asset.uri,
               mediaType: asset.mediaType,
               width: asset.width,
               height: asset.height,
               duration: asset.duration,
               creationTime: asset.creationTime,
               modificationTime: asset.modificationTime,
               mediaSubtypes: asset.mediaSubtypes,
             });
           });
         } else {
           console.log(`相册 "${album.title}" 中没有可访问的文件`);
         }
       }
       
       // 使用你提供的代码测试
       console.log('=== 使用你的代码测试 ===');
       try {
         const permissionRes = await MediaLibrary.requestPermissionsAsync(false);
         if (permissionRes.granted) {
           const albumsRes = await MediaLibrary.getAlbumsAsync({ includeSmartAlbums: true });
           console.log('Success: ', albumsRes);
           
           // 检查每个相册
           for (const album of albumsRes) {
             console.log(`智能相册 "${album.title}":`, {
               id: album.id,
               assetCount: album.assetCount,
               type: album.type
             });
           }
         } else {
           console.log('Something went wrong.');
         }
       } catch (err) {
         console.log('Error: ', err.message);
       }
      
             // 4. 尝试获取媒体文件 - 不限制类型
       console.log('=== 测试1: 获取所有类型的媒体文件 ===');
       const allAssets = await MediaLibrary.getAssetsAsync({
         first: 10,
       });
       console.log('所有类型文件总数:', allAssets.totalCount);
       console.log('获取到的所有类型文件数:', allAssets.assets.length);
       
       if (allAssets.assets.length > 0) {
         console.log('第一个文件信息:', {
           id: allAssets.assets[0].id,
           uri: allAssets.assets[0].uri,
           mediaType: allAssets.assets[0].mediaType,
           filename: allAssets.assets[0].filename,
           width: allAssets.assets[0].width,
           height: allAssets.assets[0].height,
         });
       }
       
       // 5. 尝试获取图片文件
       console.log('=== 测试2: 只获取图片文件 ===');
       const photoAssets = await MediaLibrary.getAssetsAsync({
         first: 10,
         mediaType: MediaLibrary.MediaType.photo,
       });
       console.log('图片文件总数:', photoAssets.totalCount);
       console.log('获取到的图片文件数:', photoAssets.assets.length);
       
       // 6. 尝试获取视频文件
       console.log('=== 测试3: 只获取视频文件 ===');
       const videoAssets = await MediaLibrary.getAssetsAsync({
         first: 10,
         mediaType: MediaLibrary.MediaType.video,
       });
       console.log('视频文件总数:', videoAssets.totalCount);
       console.log('获取到的视频文件数:', videoAssets.assets.length);
       
       // 7. 尝试获取图片和视频文件
       console.log('=== 测试4: 获取图片和视频文件 ===');
       const assets = await MediaLibrary.getAssetsAsync({
         first: 10,
         mediaType: [MediaLibrary.MediaType.photo, MediaLibrary.MediaType.video],
       });
       
       console.log('媒体文件总数:', assets.totalCount);
       console.log('获取到的文件数:', assets.assets.length);
       
       if (assets.assets.length > 0) {
         console.log('第一个文件信息:', {
           id: assets.assets[0].id,
           uri: assets.assets[0].uri,
           mediaType: assets.assets[0].mediaType,
           width: assets.assets[0].width,
           height: assets.assets[0].height,
         });
       }
       
       // 8. 尝试不同的查询参数
       console.log('=== 测试5: 尝试不同的查询参数 ===');
       try {
         const testAssets1 = await MediaLibrary.getAssetsAsync({
           first: 50,
           sortBy: [MediaLibrary.SortBy.creationTime],
         });
         console.log('测试5-1 结果:', testAssets1.totalCount, '个文件');
         
         const testAssets2 = await MediaLibrary.getAssetsAsync({
           first: 50,
           sortBy: [MediaLibrary.SortBy.modificationTime],
         });
         console.log('测试5-2 结果:', testAssets2.totalCount, '个文件');
         
         const testAssets3 = await MediaLibrary.getAssetsAsync({
           first: 50,
           sortBy: [MediaLibrary.SortBy.filename],
         });
         console.log('测试5-3 结果:', testAssets3.totalCount, '个文件');
       } catch (error) {
         console.log('测试5 出错:', error.message);
       }
      
      Alert.alert('调试信息', `权限: ${requestResult.status}\n相册数: ${albums.length}\n媒体文件数: ${assets.totalCount}`);
    } else {
      console.log('权限被拒绝');
      Alert.alert('权限被拒绝', '请在设置中开启相册访问权限');
    }
    
  } catch (error) {
    console.error('调试过程中出错:', error);
    Alert.alert('调试错误', error.message);
  }
};
